package com.cool.feiyan

import android.util.Log
import com.github.kittinunf.fuel.httpGet
import com.github.kittinunf.result.Result
import org.json.JSONArray
import org.json.JSONObject

class QueryData {

    private var uri : String = String()
    private var jsonString : String = String()
    private val nowHour : String

    init {
        //TODO 生成 Math.floor(new Date().getTime()/1000/3600

        nowHour = Math.floor(System.currentTimeMillis() / (1000.0 * 3600.0)).toInt().toString()
        uri = "http://2019ncov.nosugartech.com/data.json?$nowHour"
    }

    // 还有一个数据来源：https://ncov.dxy.cn/ncovh5/view/pneumonia

    public fun QueryAsync() : String {

        val Tag : String = "QueryAsync"

        val httpAsync = uri.httpGet().responseString{ request, response, result ->
            when(result) {
                is Result.Failure -> {
                    val ex = result.getException()
                    Log.d(Tag, ex.toString())
                }
                is Result.Success -> {
                    jsonString += result.get()
                }
            }
        }

        httpAsync.join()

        return jsonString
    }

    public fun QuerySync() : String {

        val Tag : String = "QuerySync"

        val (request, response, result) = uri.httpGet().responseString()
        //val (request, response, result) = Fuel.get(uri).responseString()

        when(result) {
            is Result.Failure -> {
                val ex = result.getException()
                Log.d(Tag, ex.toString())
            }
            is Result.Success -> {
                jsonString = result.get()
            }
         }

        return jsonString
    }

    public fun ParseJson() : MutableList<FeiYanData> {

        val Tag : String = "ParseJson"

        //jsonString = "{\"code\":0,\"msg\":\"\",\"data\":[{\"id\":46,\"t_date\":\"2020-01-26\",\"t_start\":\"2020-01-25T10:45:00.000Z\",\"t_end\":\"2020-01-25T16:20:00.000Z\",\"t_type\":1,\"t_no\":\"TR134\",\"t_memo\":\"\",\"t_no_sub\":\"\",\"t_pos_start\":\"新加坡\",\"t_pos_end\":\"西安\",\"source\":\"https://mp.weixin.qq.com/s/jEBlMLCgSTD9AcpcV1yRtg\",\"who\":\"央视新闻\",\"verified\":1,\"created_at\":\"2020-01-27T09:54:12.000Z\",\"updated_at\":\"2020-01-27T09:54:12.000Z\"},{\"id\":88,\"t_date\":\"2020-01-25\",\"t_start\":\"2020-01-24T16:00:00.000Z\",\"t_end\":\"2020-01-25T15:59:59.000Z\",\"t_type\":1,\"t_no\":\"8L9564\",\"t_memo\":\"\",\"t_no_sub\":\"\",\"t_pos_start\":\"杭州\",\"t_pos_end\":\"赣州\",\"source\":\"http://tt.m.jxnews.com.cn/news/91/911234.html?app=jxttn&from=groupmessage&isappinstalled=0\",\"who\":\"中国江西网-江南都市报\",\"verified\":1,\"created_at\":\"2020-01-27T11:59:02.000Z\",\"updated_at\":\"2020-01-27T11:59:02.000Z\"}]}"

        val feiYanDataList : MutableList<FeiYanData> = mutableListOf()

        val jsonObject = JSONObject(jsonString)
        val jsonArray = JSONArray(jsonObject["data"].toString())

        for (i in 0 until jsonArray.length()) {
            var feiYanData = FeiYanData() // 这里就是新建了一个对象，所以就不用再使用 deepCopy 了。
            val item = jsonArray.getJSONObject(i)
            feiYanData.id = item["id"].toString().toInt()
            feiYanData.日期 = item["t_date"].toString().trim()
            feiYanData.开始时间 = item["t_start"].toString().trim()
            feiYanData.结束时间 = item["t_end"].toString().trim()
            feiYanData.交通类型 = 交通类型(item["t_type"].toString().trim())
            feiYanData.车次_车牌_航班号_场所名 = item["t_no"].toString().trim()
            feiYanData.车次附加描述 = item["t_memo"].toString().trim()
            feiYanData.车厢 = item["t_no_sub"].toString().trim()
            feiYanData.出发站 = item["t_pos_start"].toString().trim()
            feiYanData.到达站 = item["t_pos_end"].toString().trim()
            feiYanData.线索来源名称 = item["who"].toString().trim()
            feiYanData.线索来源网址 = item["source"].toString().trim()
            feiYanData.提交时间 = item["created_at"].toString().trim()
            feiYanData.更新时间 = item["updated_at"].toString().trim()

            if(feiYanData.日期.isEmpty())
                feiYanData.日期 = "无"

            if(feiYanData.开始时间.isEmpty())
                feiYanData.开始时间 = "无"

            if(feiYanData.结束时间.isEmpty())
               feiYanData.结束时间 = "无"

            if(feiYanData.交通类型.isEmpty())
               feiYanData.交通类型 = "无"

            if(feiYanData.车次_车牌_航班号_场所名.isEmpty())
               feiYanData.车次_车牌_航班号_场所名 = "无"

            if(feiYanData.车次附加描述.isEmpty())
               feiYanData.车次附加描述 = "无"

            if(feiYanData.车厢.isEmpty())
               feiYanData.车厢 = "无"

            if(feiYanData.出发站.isEmpty())
               feiYanData.出发站 = "无"

            if(feiYanData.到达站.isEmpty())
               feiYanData.到达站 = "无"

            if(feiYanData.线索来源名称.isEmpty())
               feiYanData.线索来源名称 = "无"

            if(feiYanData.线索来源网址.isEmpty())
                feiYanData.线索来源网址 = "无"

            if(feiYanData.提交时间.isEmpty())
               feiYanData.提交时间 = "无"

            if(feiYanData.更新时间.isEmpty())
               feiYanData.更新时间 = "无"

            if (item["verified"].toString().equals("1"))
               feiYanData.验证通过 = true
            else
                feiYanData.验证通过 = false

            feiYanDataList.add(feiYanData)
        }

        return feiYanDataList
    }

    private fun 交通类型(id : String) : String {
        return when(id) {
            "1" -> "飞机"
            "2" -> "火车"
            "3" -> "地铁"
            "4" -> "长途客车"
            "5" -> "公交车"
            "6" -> "出租车"
            "7" -> "轮船"
            else -> "其它公共场合"
        }
    }
}
